import com.top.em.MenuEnum;
import com.top.em.TipEnum;
import com.top.entiy.Food;
import com.top.entiy.OrderItem;
import com.top.entiy.User;
import com.top.service.FoodService;
import com.top.service.OrderService;
import com.top.utils.FormatUtil;
import com.top.utils.SoutUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Author:
 * @DateTime: 2023/12/10 08:59
 * @Description: 客户端
 */
public class ClientMain {
    private OrderService orderService = new OrderService();
    private FoodService foodService = new FoodService();
    // 当前已点
    private static ArrayList<OrderItem> foodList;

    public static void main(String[] args) {
        ClientMain clientMain = new ClientMain();
        clientMain.index();
    }

    private void index() {
        foodList = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        System.out.println("------------点餐系统----------");
        System.out.println("请输入桌号：");
        String number = scanner.next();
        selectNumber(Integer.valueOf(number));
    }

    /**
     * 选择桌号
     *
     * @param integer
     */
    private void selectNumber(Integer integer) {
        int count = orderService.countByTableNoActive(integer);
        if (count > 0) {
            System.out.println("当前" + integer + "号桌已点餐,是否继续添加菜品");
            Scanner scanner = new Scanner(System.in);
            System.out.println(TipEnum.COMMON.getTip());
            SoutUtil.printButton(MenuEnum.CONTINUE);
            String select = scanner.next();
            try {
                if (select.equals("C")) {
                    orderFood(integer);
                } else if (select.equals("Q")) {
                    index();
                } else {
                    System.err.println("输入错误");
                    selectNumber(integer);
                }
            } catch (Exception e) {
                System.err.println("输入错误");
                selectNumber(integer);
            }

        } else {
            System.out.println("您选择的" + integer + "号桌是空闲的");
            // 点餐
            orderFood(integer);
        }
    }

    /**
     * 点餐
     *
     * @param tableNo 桌号
     */
    private void orderFood(Integer tableNo) {
        System.out.println("----------" + tableNo + "号桌已点菜品----------");
        // 已点菜 list
        System.out.println(FormatUtil.format(10, "菜品名称", "单价", "数量", "总价格"));
        for (OrderItem orderItem : foodList) {
            System.out.println(FormatUtil.format(10, orderItem.getFoodName(), orderItem.getPrice(), orderItem.getNum(), orderItem.getAmount()));
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择操作：");
        SoutUtil.printButton(MenuEnum.SELECT, MenuEnum.SUBMIT);
        String next = scanner.next();
        try {
            if (next.equals("S")) {
                System.out.println("---------菜单------------");
                foodService.menu();
                System.out.println("请输入菜品ID");
                int foodId = scanner.nextInt();
                System.out.println("请输入数量");
                int num = scanner.nextInt();
                OrderItemBuilder(tableNo, foodId, num);
                orderFood(tableNo);
            } else if (next.equals("B")) {
                submit(tableNo);
            } else if (next.equals("Q")) {
                index();
            } else {
                System.err.println("输入错误");
                orderFood(tableNo);
            }
        } catch (Exception e) {
            System.err.println("输入错误");
            orderFood(tableNo);
        }
    }

    /**
     * 提交订单
     *
     * @param tableNo 桌号
     */
    private void submit(Integer tableNo) {
        if (foodList.size() == 0) {
            System.out.println("请先点菜");
            orderFood(tableNo);
        } else {
            System.out.println("----------" + tableNo + "号桌已点菜品----------");
            System.out.println(FormatUtil.format(10, "菜品名称", "单价", "数量", "总价格"));
            BigDecimal total = BigDecimal.ZERO;
            for (OrderItem orderItem : foodList) {
                total = total.add(orderItem.getAmount());
                System.out.println(FormatUtil.format(10, orderItem.getFoodName(), orderItem.getPrice(), orderItem.getNum(), orderItem.getAmount()));
            }
            boolean b = orderService.createOrder(foodList, tableNo);
            System.out.println("--------提交成功总价为" + total + "-------");
            // 清空已点菜
            foodList = new ArrayList<>();
        }
    }


    /**
     * 已点菜品数据构建
     *
     * @param foodId
     * @param num
     */
    private void OrderItemBuilder(Integer tableNo, int foodId, int num) {
        Food food = foodService.findById(foodId);
        if (food == null || food.getEnable() == 0) {
            System.err.println("选择的菜品不存在");
            orderFood(tableNo);
        }
        OrderItem orderItem = new OrderItem();
        orderItem.setFoodId(foodId);
        orderItem.setFoodName(food.getName());
        orderItem.setNum(num);
        orderItem.setPrice(food.getPrice());
        orderItem.setAmount(new BigDecimal(num).multiply(food.getPrice()));
        if (foodList != null && foodList.size() > 0) {
            boolean contains = foodList.contains(orderItem);
            if (contains){
                // 如果已点过,则数量+num
                for (OrderItem item : foodList) {
                    if (item.equals(orderItem)) {
                        // 如果已点过,则数量+num
                        item.setNum(item.getNum() + num);
                        item.setAmount(new BigDecimal(item.getNum()).multiply(food.getPrice()));
                    }
                }
            }else {
                // 如果没有点过,则添加
                foodList.add(orderItem);
            }
        } else {
            foodList.add(orderItem);
        }

    }
}
